<script type="text/javascript" charset="utf-8" src="__PUBLIC__/Common/js/ueditor/ueditor.config.js"></script>
<script type="text/javascript" charset="utf-8" src="__PUBLIC__/Common/js/ueditor/ueditor.all.js"></script>
<div class="modal fade" id="edit-notice" tabindex="-1">
    <div class="modal-dialog">
        <form id="edit_notice_form" name="edit_notice_form" method="post" action="/set/notice/save">
            <input type="hidden" name="id" value=""/>
            <input type="hidden" name="status" value="0"/>
            <input type="hidden" name="curren_status" value="0"/>
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
                    <h4 class="modal-title" id="batch-title">编辑通知信息</h4>
                </div>
                <div class="modal-body">
                    <div class="form-group">
                        <label>通知标题<span class="text-important">*</span></label>
                        <input name="title" class="form-control" type="text" value="">
                    </div>
                    <div class="row">
                        <div class="form-group  col-sm-8">
                            <label>发布单位<span class="text-important">*</span></label>
                            <input name="promulgator" class="form-control" type="text" value="">
                        </div>

                        <div class="form-group  col-sm-2">
                            <label>级别<span class="text-important">*</span></label>
                            <div class="dropdown dropdown-block">
                                <a class="btn btn-default" data-toggle="dropdown"><span name="rank_name" class="option-name">省级</span> <span
                                        class="caret"></span>
                                </a>
                                <ul class="dropdown-menu dropdown-option-menu">
                                    <li><a data-value="0" role="button">全部</a></li>
                                    <li><a data-value="1" role="button">省级</a></li>
                                    <li><a data-value="2" role="button">市级</a></li>
                                    <li><a data-value="3" role="button">区级</a></li>
                                </ul>
                                <input name="rank" class="option-value" type="hidden" value="1">
                            </div>
                        </div>
                        <div class="form-group  col-sm-2">
                            <label>属性<span class="text-important">*</span></label>
                            <div class="dropdown dropdown-block">
                                <a class="btn btn-default" data-toggle="dropdown"><span name="attribute_name" class="option-name">全部</span> <span
                                        class="caret"></span>
                                </a>
                                <ul class="dropdown-menu dropdown-option-menu">
                                    <li><a data-value="0" role="button">全部</a></li>
                                    <li><a data-value="1" role="button">单位</a></li>
                                    <li><a data-value="2" role="button">学校</a></li>
                                    <li><a data-value="3" role="button">社区</a></li>
                                </ul>
                                <input name="attribute" class="option-value" type="hidden" value="0">
                            </div>
                        </div>

                    </div>
                    <div class="form-group col-sm-8">
                        <label>附件<span class="text-muted">:&nbsp&nbsp</span>
                            &nbsp&nbsp
                            <span class="text-muted" id="annex_span">
                                                    <a target="_blank" class="linkannex" href="">
                                                    </a>
                                                </span>

                        </label>
                        <div id="ossfile">你的浏览器不支持flash,Silverlight或者HTML5！</div>
                        <br/>
                        <div id="container">
                            <a id="selectfiles" href="javascript:void(0);" class='btn'>选择文件</a>( 提示：保存后新上传的附件会替换原附件)
                            <a id="postfiles" href="javascript:void(0);" class='btn' style="display:none">开始上传</a>
                        </div>
                        <div id="console"></div>
                    </div>
                    <div class="form-group">
                        <label>通知内容<span class="text-important">*</span>( 提示：正文一次最多可输入8000字)</label>
                        <!--<textarea name="content" class="form-control" type="text" style="height:120px"></textarea>-->
                        <input name="content" type="hidden" value="">
                        <p id="editor" type="text/plain" style="width:568px;height:300px;"></p>
                    </div>

                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                    <button type="submit" class="btn btn-primary" action-type="draft">保存草稿</button>
                    <button type="button" class="btn btn-primary" action-type="publish">发布通知</button>
                </div>
            </div>
        </form>
        <form name=theform style="display:none">
            <input type="radio" name="myradio" value="random_name" checked=true/>
        </form>
    </div>
</div>
<script type="text/javascript" src="/up/lib/plupload-2.1.2/js/plupload.full.min.js"></script>
<script type="text/javascript" src="/up/upload_notice.js"></script>
<script type="text/javascript">
    $(document).ready(function () {
        $('#nav-set').addClass("active");
        $("#ossfile").delegate("a", "click", function () {
            var r1 = $(this).parent().parent();
            for (var i in uploader.files) {
                if (uploader.files[i].id == r1.attr('id')) {
                    uploader.files.splice(i, 1)
                }
            }
            r1.remove();
        });

        $('.deleteannex').click(function () {
            var $this = $(this);
            var aid = $(this).data('aid');
            alertify.confirm("确定删除此附件?", function () {
                $.post('/task/index/deleteannex', 'id=' + aid, function (data) {
                    if (data === "0") {
                        $this.parent().remove()
                    }
                }).error(function (msg, url, line) {
                    alert(msg.responseText);
                });
            });
        });

        $('[action-type="publish"][type="button"]').click(function () {
            $('#edit_notice_form input[name="status"]').val('1');
            $('#edit_notice_form').submit();
        });

        $('[data-toggle="modal"][data-target="#edit-notice"]').click(function () {
            reset_edit_notice_form();
            var $this = $(this);
            var node = $this.parent().parent();

            edit_edit_notice_form(node);
        });

        $("#edit_notice_form").validate({
            submitHandler: function () {
                $('#edit_notice_form button[action-type="draft"]').attr("disabled", "true");
                $.post($("#edit_notice_form").attr("action"), $("#edit_notice_form").serialize(), function (data) {
                    if (data === "0") {
                        alertify.alert("保存成功!", function () {
                            $('#edit_notice_form').modal('hide');
                            location.reload();
                        });
                    } else {
                        alertify.alert(data);
                    }
                }).error(function (msg, url, line) {
                    alert(msg.responseText);
                });
            },
            ignore: "",
            rules: {
                title: {
                    required: true
                },
                promulgator: {
                    required: true
                },
                content: {
                    required: true
                }
            },
            messages: {
                title: {
                    required: "请填写通知标题"
                },
                promulgator: {
                    required: "请填写发布单位"
                },
                content: {
                    required: "请填写通知内容"
                }
            }
        });
    });

    var ue = UE.getEditor('editor');
    ue.addListener('blur', function (editor) {
        $('#edit_notice_form input[name="content"]').val(ue.getContent());
    });

    function reset_edit_notice_form() {
        $('#edit_notice_form input[name="id"]').val('');
        $('#edit_notice_form input[name="status"]').val('0');
        $('#edit_notice_form input[name="curren_status"]').val('0');
        $('#edit_notice_form input[name="title"]').val('');
        $('#edit_notice_form input[name="promulgator"]').val('');
        $('#edit_notice_form input[name="content"]').val('');
        $('#annex_span a').html('');
        $('#annex_span a').attr('href', '');
        $('#ossfile').html('');
        init_select($('#edit_notice_form input[name="rank"]'), $('#edit_notice_form span[name="rank_name"]'), 1);
        init_select($('#edit_notice_form input[name="attribute"]'), $('#edit_notice_form span[name="attribute_name"]'), 1);
        ue.setContent('');

        $('#edit_notice_form button[action-type="draft"]').show();
        $('#edit_notice_form span[name="rank_name"]').parent().attr('data-toggle', 'dropdown');
        $('#edit_notice_form span[name="rank_name"]').next().show();

        $('#edit_notice_form span[name="attribute_name"]').parent().attr('data-toggle', 'dropdown');
        $('#edit_notice_form span[name="attribute_name"]').next().show();
    }

    function edit_edit_notice_form(node) {
        $('#edit_notice_form input[name="id"]').val(node.data('vid'));
        $('#edit_notice_form input[name="status"]').val(node.data('status'));
        $('#edit_notice_form input[name="curren_status"]').val(node.data('status'));
        $('#edit_notice_form input[name="title"]').val(node.data('title'));
        $('#edit_notice_form input[name="promulgator"]').val(node.data('promulgator'));
        $('#edit_notice_form input[name="content"]').val(node.data('content'));
        $('#annex_span a').html(node.data('annex_name'));
        $('#annex_span a').attr('href', node.data('annex_path'));
        init_select($('#edit_notice_form input[name="rank"]'), $('#edit_notice_form span[name="rank_name"]'), node.data('rank'));
        init_select($('#edit_notice_form input[name="attribute"]'), $('#edit_notice_form span[name="attribute_name"]'), node.data('attribute'));
        ue.setContent(node.data('content'));

        if (node.data('status') == '1') {
            $('#edit_notice_form button[action-type="draft"]').hide();
            $('#edit_notice_form span[name="rank_name"]').parent().removeAttr('data-toggle');
            $('#edit_notice_form span[name="rank_name"]').next().hide();

            $('#edit_notice_form span[name="attribute_name"]').parent().removeAttr('data-toggle');
            $('#edit_notice_form span[name="attribute_name"]').next().hide();
        }
    }

    function init_select(ctr, ctr_name, data) {
        ctr.val(data);
        var vx = ctr.val();
//        if (vx != "") {
        ctr.prev().find('a').each(function () {
            var $this = $(this);
            if ($this.data('value') == vx) {
                ctr_name.text($this.text());
            }
        });
//        }
    }
</script>